package cn.mumzone.pos.presenter;

import android.util.ArrayMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.pos.model.HelpCenterChildEntity;
import cn.mumzone.pos.model.HelpCenterEntity;
import cn.mumzone.pos.model.source.AppRepository;
import cn.mumzone.pos.presenter.contract.HelpCenterContract;

/**
 * Created by JoeLjt on 2019/11/19.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public class HelpCenterPresenter extends AbsPresenter<HelpCenterContract.View, AppRepository> {

    public void getHelpCenterTitle() {
        ApiCallBack<HelpCenterEntity> callBack = new ApiCallBack<HelpCenterEntity>() {
            @Override
            public void onSuccess(HelpCenterEntity response) {
                mView.dismissLoadingDialog();
                mView.updateTitle(getRightfulParentTitle(response), getRightfulMap(response));
            }

            @Override
            public void onError(BaseResponse response) {
                mView.dismissLoadingDialog();
                mView.showError(response.getMsg());
            }

            @Override
            public void onNetworkError(BaseResponse response) {
                mView.dismissLoadingDialog();
                mView.showNetError();
            }

        };

        mView.showLoadingDialog();
        mRepo.getHelpCenterData(callBack);
        addSubscrebe(callBack);
    }

    private List<String> getRightfulParentTitle(HelpCenterEntity response) {
        ArrayList<String> keyList = new ArrayList<>();
        if (ListUtil.isListNotEmpty(response.getList())) {
            for (HelpCenterEntity.GroupBean entity : response.getList()) {
                List<HelpCenterEntity.GroupBean.PointsBean> childResponseList = entity.getPoints();
                if (!ListUtil.isListEmpty(childResponseList)) {
                    keyList.add(entity.getDirectoryName());
                }
            }
        }
        return keyList;
    }

    private ArrayMap<String, List<HelpCenterChildEntity>> getRightfulMap(HelpCenterEntity response) {
        ArrayMap<String, List<HelpCenterChildEntity>> map = new ArrayMap<>();
        if (ListUtil.isListNotEmpty(response.getList())) {
            for (HelpCenterEntity.GroupBean entity : response.getList()) {
                List<HelpCenterEntity.GroupBean.PointsBean> childResponseList = entity.getPoints();
                if (!ListUtil.isListEmpty(childResponseList)) {
                    ArrayList<HelpCenterChildEntity> childList = new ArrayList<>();
                    for (HelpCenterEntity.GroupBean.PointsBean child : childResponseList) {
                        HelpCenterChildEntity childEntity = new HelpCenterChildEntity();
                        childEntity.setChildTitle(child.getKnowledgePointName());
                        childEntity.setChildContent(child.getContent());
                        childList.add(childEntity);
                    }
                    map.put(entity.getDirectoryName(), childList);
                }
            }
        }
        return map;
    }

}
